package com.guigu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.pojo.SupplierProduct;
import com.guigu.service.SupplierProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.pojo.SupplierProduct;
import com.guigu.service.SupplierProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 * 供货商商品关联表 前端控制器
 * </p>
 *
 * @author Mr.Zhang
 * @since 2025-03-12
 */
@CrossOrigin
@RestController
@RequestMapping("/supplier-product")
public class SupplierProductController {

    @Autowired
    private SupplierProductService supplierProductService;

    @PostMapping("/getSupplierProductServiceByProductId")
    @ResponseBody
    public Page<SupplierProduct> getSupplierProductServiceByProductId(@RequestBody HashMap<String, Object> map) {

        Page<SupplierProduct> supplierProductServiceByProductId = supplierProductService.getSupplierProductServiceByProductId(map);

        if (supplierProductServiceByProductId != null) {
            return supplierProductServiceByProductId;
        }
        return null;
    }

    @PostMapping("/saveSupplierProduct")
    @ResponseBody
    public boolean saveSupplierProduct(SupplierProduct supplierProduct) {
        return supplierProductService.saveOrUpdate(supplierProduct);
    }

    @PostMapping("/getSupplierProductBySupplierIdAndProductId")
    @ResponseBody
    public SupplierProduct getSupplierProductBySupplierIdAndProductId(Integer supplierId,Integer productId) {
        QueryWrapper<SupplierProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("supplier_id",supplierId);
        queryWrapper.eq("product_id",productId);
        if (supplierProductService.getOne(queryWrapper)!=null){
            return supplierProductService.getOne(queryWrapper);
        }
        return null;
    }

    @PostMapping("/getSupplierProductBySupplierId")
    @ResponseBody
    public Page<SupplierProduct> getSupplierProductBySupplierId(Integer supplierId,Integer currentPage,Integer pageSize ){
        Page<SupplierProduct> page = supplierProductService.getSupplierProductBySupplierId(supplierId,currentPage,pageSize);
        return page;
    }

    @PostMapping("/UpdateSupplierProduct")
    @ResponseBody
    public Boolean UpdateSupplierProduct(SupplierProduct supplierProduct){
        return supplierProductService.updateById(supplierProduct);
    }

    @PostMapping("/removeSupplierProductById")
    @ResponseBody
    public Boolean removeSupplierProductById(Integer id){
        return supplierProductService.removeById(id);
    }
}

